<?php
class ReceiptPlugin extends BasePlugin {
	
	protected $name = '回执确认';
	protected $type = 'single';
	protected $config = array ('cid' => 2082, 'mid' => 1, 'num' => 3 );
	static $receiptService;
	/* (non-PHPdoc)
	 * @see IPlugin::api()
	 */
	public function api() {
		$rs = self::getReceiptService ()->save ( $_POST ['nid'], $this->_user_global ['uid'] );
		if ($rs == self::SUCCESS) {
			self::getLogService ()->add ( $this->_user_global ['username'], "回执确认成功$_POST[nid]" );
		} else {
			self::getLogService ()->add ( $this->_user_global ['username'], "回执确认失败$_POST[nid]" );
		}
		$this->sendNotice ( $rs );
	}
	
	public function v() {
	}
	/* 
	 * 显示通知公告
	 * @see IPlugin::run()
	 */
	public function run() {
		$user = self::getReceiptService ()->getReceiptByUid ( $this->_user_global ['uid'] );
		$rs = self::getNewsService ()->listing ( $this->config ['cid'], 1, 'id', $this->config ['num'] );
		
		$isRead = array ();
		foreach ( $user as $uv ) {
			$isRead [] = $uv ['nid'];
		}
		
		$list = $rs ['newslist'];
		foreach ( $list as $k => $v ) {

			if (in_array ( $v ['id'], $isRead )) {
				$list [$k] ['read'] = 1;
			} else {
				$list [$k] ['read'] = 0;
			}
			
			$list [$k] ['readnum'] = self::getReceiptService ()->getNumByNid ( $v ['id'] );
		
		}
		
		$this->view ()->assign ( 'receipt', $list );
	}

	
	public function getNewsService() {
		return new NewsService ( $this->config ['cid'], $this->config ['mid'] );
	}
	
	public static function getReceiptService() {
		
		if (self::$receiptService == null) {
			self::$receiptService = new ReceiptService ();
		}
		return self::$receiptService;
	
	}

}

//回执功能
class ReceiptService {
	const SUCCESS = 'success';
	const ERROR = 'error';
	
	private $_status = array (0 => '未操作', 1 => '赞成', 2 => "反对" );
	
	/*添加回执状态*/
	public function save($nid, $uid) {
		
		if (! $this->checkIsExsit ( $uid, $nid )) {
			return "已经确认过了";
		}
		
		$rs = ReceiptModel::instance ()->addReceipt ( array ('nid' => $nid, 'uid' => $uid, 'add_time' => time () ) );
		return $rs > 0 ? self::SUCCESS : self::ERROR;
	}
	
	public function getNumByNid($nid) {
		$num = ReceiptModel::instance ()->getNumByNid ( $nid );
		return $num ['num'];
	}
	/**
	 * 
	 * @param int $uid
	 * @param int $nid
	 * @return Boolean true 不存在 false 存在
	 */
	public function checkIsExsit($uid, $nid) {
		$rs = ReceiptModel::instance ()->getReceiptByWhere ( array ('uid' => $uid, 'nid' => $nid ) );
		return empty ( $rs );
	}
	
	public function getReceiptByNid($nid) {
		return ReceiptModel::instance ()->getReceiptByWhere ( array ('nid' => $nid ) );
	}
	
	public function getReceiptByUid($uid) {
		return ReceiptModel::instance ()->getReceiptByWhere ( array ('uid' => $uid ) );
	}
}

class ReceiptModel extends Db {
	
	private $_receipt = 'd_receipt';
	
	public function addReceipt($params) {
		return $this->add ( $this->_receipt, $params );
	}
	
	public function getReceiptByWhere($where) {
		return $this->getAll ( $this->_receipt, $where );
	}
	
	public function getNumByNid($nid) {
		$sql = "SELECT count(id) num FROM $this->_receipt WHERE nid=$nid";
		return $this->fetch ( $sql );
	}
	
	/**
	 * 
	 * @return ReceiptModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}
}

